//
//  MLASavePinPutOutVC.m
//  MagicLampApp
//
//  Created by Zheng on 2018/12/24.
//  Copyright © 2018 zheng.magic.lamp.app. All rights reserved.
//

#import "MLASavePinPutOutVC.h"
#import "MLABaseSearchView.h"
#import "MLASavePinPutOutCell.h"
#import "MLASavePinCateVC.h"
#import "MLASavePinPutInOutDetailVC.h"
#import "MLASavePinPutInOutModel.h"

@interface MLASavePinPutOutVC () {
    MLABaseSearchView *searchView;
    UILabel *cateLbl;
    NSString *keyword;
    NSString *type;
    NSMutableArray<MLASavePinPutInOutModel *> *modelsArr;
}

@end

@implementation MLASavePinPutOutVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    keyword = @"";
    type = @"";
    [self configTitle:@"出库记录"];
    
    searchView = [[MLABaseSearchView alloc] init];
    [self.view addSubview:searchView];
    [searchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.titleView.mas_bottom);
        make.height.mas_equalTo(80);
    }];
    
    UIControl *cateCtrl = [[UIControl alloc] init];
    [cateCtrl addTarget:self action:@selector(cateCtrlAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:cateCtrl];
    [cateCtrl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self->searchView.mas_bottom);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(30);
        make.width.mas_equalTo(100);
    }];
    
    UIImageView *markImgv = [[UIImageView alloc] init];
    markImgv.image = [UIImage imageNamed:@"bot_arrow_icon"];
    [cateCtrl addSubview:markImgv];
    [markImgv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(9);
        make.height.mas_equalTo(5);
        make.right.centerY.equalTo(cateCtrl);
    }];
    
    cateLbl = [[UILabel alloc] init];
    cateLbl.textColor = CX_GetColorTitle();
    cateLbl.font = CX_GetNormalFont(14);
    cateLbl.text = @"类别";
    [cateCtrl addSubview:cateLbl];
    [cateLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(markImgv.mas_left).offset(-8);
        make.centerY.equalTo(cateCtrl);
    }];
    
    
    [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(self.view);
        make.top.equalTo(self->searchView.mas_bottom).offset(29);
    }];
    
    self.tableView.rowHeight = 127.f;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerClass:[MLASavePinPutOutCell class] forCellReuseIdentifier:kPutOutCellID];
    
    self.haveRefreshHeader = YES;
    [self loadData];
    [self addClickActions];

}

#pragma mark - LoadData
- (void)loadData {
    [super loadData];
    
    NSDictionary *params = @{@"name":keyword,
                             @"type":@"2",
                             @"type_id":type};
    
    [MLAHttpManage postHostType:MLAHttpHostType_Jinxiaocun api:kPutInOutList params:params success:^(id  _Nonnull response, NSString * _Nonnull msg) {
        self.page++;
        self.haveRefreshFooter = YES;
        self->modelsArr = [NSMutableArray array];
        self->modelsArr = [MLASavePinPutInOutModel mj_objectArrayWithKeyValuesArray:response[@"lists"]];
        if (self->modelsArr.count < 20) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
        [self.tableView reloadData];
        
    } failure:^{
        
    }];
}

- (void)loadMoreData {
    NSDictionary *params = @{@"page":@(self.page),
                             @"name":keyword,
                             @"type":@"2",
                             @"type_id":type};
    [MLAHttpManage postHostType:MLAHttpHostType_Jinxiaocun api:kPutInOutList params:params success:^(id  _Nonnull response, NSString * _Nonnull msg) {
        
        self.page++;
        
        if ([response[@"lists"] count] < 20) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        } else {
            [self.tableView.mj_footer endRefreshing];
        }
        
        [self->modelsArr addObjectsFromArray:[MLASavePinPutInOutModel mj_objectArrayWithKeyValuesArray:response[@"lists"]]];
        [self.tableView reloadData];
        
    } failure:^{
        
    }];
}

#pragma mark - tableview datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return modelsArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MLASavePinPutOutCell *cell = [tableView dequeueReusableCellWithIdentifier:kPutOutCellID forIndexPath:indexPath];
    MLASavePinPutInOutModel *model = modelsArr[indexPath.row];
    cell.model = model;

    return cell;
}

#pragma mark - Actions
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    CX_Log(@"出库详情");
    MLASavePinPutInOutModel *model = modelsArr[indexPath.row];
    MLASavePinPutInOutDetailVC *detailVC = [[MLASavePinPutInOutDetailVC alloc] init];
    detailVC.type = @"2";
    detailVC.model = model;
    [self.navigationController pushViewController:detailVC animated:YES];
}


- (void)cateCtrlAction {
    CX_Log(@"类别选择");
    
    MLASavePinCateVC *cateVC = [[MLASavePinCateVC alloc] init];
    [self.navigationController pushViewController:cateVC animated:YES];
    
    cateVC.typeSelectBlock = ^(NSString * _Nonnull typeStr, NSString * _Nonnull typeId) {
        self->cateLbl.text = typeStr;
        self->type = typeId;
        [self loadData];
    };

}


- (void)addClickActions {
    @weakify(self);
    searchView.editChangeBlock = ^(NSString * _Nonnull keyword) {
        @strongify(self);
        self->keyword = keyword;
        [self loadData];
    };
}


@end
